package main

import (
	"fmt"
	"sort"
)

func printSlice(x []int) {
	fmt.Printf("len=%d cap=%d slice=%v\n", len(x), cap(x), x)
}

func main() {
	//1.切片
	//定义切片，make([]type, len, cap) 切片类型，长度，容量(可写可不写)
	var numbers = make([]int, 3, 5)
	//printSlice(numbers)
	numbers = []int{1, 2, 3}
	//printSlice(numbers)
	fmt.Println(numbers)

	//切片截取与python一样,不多赘述
	numbers1 := numbers[:2]
	// printSlice(numbers1)

	//append()向切片添加元素，可以添加多个,也可以是切片但是要加...
	numbers = append(numbers, numbers1...)
	//printSlice(numbers)

	//copy函数，第二个切片复制到第一个切片
	numbers2 := make([]int, len(numbers), (cap(numbers))*2)
	copy(numbers2, numbers)
	//printSlice(numbers2)

	//没有删除函数，只能通过断点连接
	var number3 = []int{0, 1, 2, 3, 4, 5, 6, 7, 8}
	//printSlice(number3)
	i := 3
	number3 = append(number3[:i], number3[i+1:]...)
	//printSlice(number3)

	//排序sort 默认从小到大
	number4 := []int{98, 12, 23, 45, 43, 2, 483}
	sort.Ints(number4)
	//printSlice(number4)
	sort.Sort(sort.Reverse(sort.IntSlice(number4)))
	//printSlice(number4)

	//2. map集合
	//定义map  类似于python的dic，但是map是无序的,且必须初始化,两种初始化
	//citymap = make(map[string]string)
	citymap := map[string]string{"厦门": "福建"}
	//        键 ----  值
	citymap["广州"] = "广东"
	citymap["贵阳"] = "贵州"
	citymap["大连"] = "辽宁"
	//不存在则添加，存在就修改

	fmt.Println(citymap)
	//遍历
	// for city := range citymap {
	// 	fmt.Println(city, "-", citymap[city])
	// }
	//查找在键在map中是否存在，返回为值和true/false
	//city1, tf := citymap["大连"]
	//fmt.Println(city1)
	//fmt.Println(tf)

	//删除delete() 只能由键值删除
	// delete(citymap, "厦门")
	// for city := range citymap {
	// 	fmt.Println(city, "-", citymap[city])
	// }

	//创建一个切片，元素为map
	var test = make([]map[int]int, 3, 3)
	if test[0] == nil {
		test[0] = make(map[int]int)
		test[0][1] = 10
	}
	fmt.Println(test)

	//创建一个map,值为切片
	var demo = make(map[int][]string)
	demo[1] = []string{"one", "ONE", "First"}
	fmt.Println(demo)
}
